package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.AssignBean;
import com.ymatou.iapi.sellercs.parameter.MessageAttach;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageWithAttachBean;
import com.ymatou.iapi.sellercs.service.AssignCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtCssession;
import com.ymttest.database.model.YmtQueuesession;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/**
 * Assign FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Assign {
	private static AssignBean assignBean;
	private static AssignCall assignCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("Assign分配任务job");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		assignBean = new AssignBean();
		assignCall = new AssignCall();
		YmtSellerCSIMWapper = new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(assignCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(SendMessageBean SendMessageBean, Integer unReadCount) throws InterruptedException {
		YmtCssession info = YmtSellerCSIMWapper.selectYmtCssession(SendMessageBean.getToUserId(), SendMessageBean.getFromUserId()).get(0);
		Logger.debug("YmtQueuesession:" + FormatUtil.beanToGSONString(info));
		Logger.verifyNotNull(info.getBeginTime(), "验证BeginTime");
		Logger.verifyEquals(13, info.getFromPlatform(), "来源平台-固定13");
		Logger.verifyEquals(true, new Date().getTime() - info.getLastMessageTime().getTime() < 10000, "LastMessageTime-误差内");
		Logger.verifyEquals(SendMessageBean.getToUserId(), info.getMasterCSId(), "验证MasterCSId");
		Logger.verifyNotNull(info.getSessionNumber(), "验证SessionNumber");
		Logger.verifyEquals(unReadCount, info.getUnReadCount(), "unReadCount");
		// 0 User, 1 ReAssign, 2 CS
		Logger.verifyEquals(2, info.getOpenType(), "OpenType-2分配打开会话");
		Logger.verifyEquals(null, info.getCloseType(), "CloseType-没有关闭时未设置");
		Logger.verifyEquals(0, info.getStatus(), "Status-0开启");
		Logger.verifyEquals(0, info.getMark(), "Mark-0");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Assign_001() {
		Logger.start(true, "调用job成功,自动分配会话");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			// 触发分配
			assignBean.setAppId("autotest");
			assignCall.setData(assignBean);
			assignCall.callService();
			Logger.verifyEquals("ok", assignCall.getReturnData(), "验证返回");
			Thread.sleep(5000);
			verifyDB(sendmessageBean, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_Assign_002() {
		Logger.start(true, "没有在线客服,不操作");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			// 触发分配
			assignBean.setAppId("autotest");
			assignCall.setData(assignBean);
			assignCall.callService();
			Logger.verifyEquals("ok", assignCall.getReturnData(), "验证返回");
			Thread.sleep(5000);
			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			Logger.verifyEquals(0, cinfos.size(), "验证没有分配");
			List<YmtQueuesession> qinfo = YmtSellerCSIMWapper.selectYmtQueuesession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			Logger.verifyEquals(1, qinfo.size(), "验证还在排队");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_Assign_003() {
		Logger.start(true, "多个客服分配找接入量最少的客服分配会话");
		try {
			Integer msc1=sellerId;
			Integer msc2=zsellerId;
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			Integer userId = urrc.getUserid();
			
			regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			Integer userId2 = urrc.getUserid();
			
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(msc1);
			// APP上线
			SellerCSIMCallService.AppLogin(msc1);
			SellerCSIMCallService.AppLogin(msc2);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, msc2, "connect");
			sendmessageBean.setToUserId(msc1);
			SellerCSIMCallService.SendMessage(sendmessageBean);
			// 触发分配
			assignBean.setAppId("autotest");
			assignCall.setData(assignBean);
			assignCall.callService();
			Logger.verifyEquals("ok", assignCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			
			
			SendMessageBean sendmessageBean2 = SellerCSIMCallService.initSendmessageBean(userId2, msc1, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean2);
			assignCall.callService();
			Logger.verifyEquals("ok", assignCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			YmtCssession info = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean2.getToUserId(), sendmessageBean2.getFromUserId()).get(0);
			Logger.debug("YmtQueuesession:" + FormatUtil.beanToGSONString(info));
			Logger.verifyNotNull(info.getBeginTime(), "验证BeginTime");
			Logger.verifyEquals(13, info.getFromPlatform(), "来源平台-固定13");
			Logger.verifyEquals(true, new Date().getTime() - info.getLastMessageTime().getTime() < 10000, "LastMessageTime-误差内");
			Logger.verifyEquals(sendmessageBean2.getToUserId(), info.getMasterCSId(), "验证MasterCSId");
			Logger.verifyNotNull(info.getSessionNumber(), "验证SessionNumber");
			Logger.verifyEquals(1, info.getUnReadCount(), "unReadCount");
			// 0 User, 1 ReAssign, 2 CS
			Logger.verifyEquals(2, info.getOpenType(), "OpenType-2分配打开会话");
			Logger.verifyEquals(null, info.getCloseType(), "CloseType-没有关闭时未设置");
			Logger.verifyEquals(0, info.getStatus(), "Status-0开启");
			Logger.verifyEquals(0, info.getMark(), "Mark-0");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}